package com.prs.crm.service.doc.impl;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;

import com.prs.crm.context.AppContext;
import com.prs.crm.context.ToolKits;
import com.prs.crm.dao.doc.ShareDeptDocDao;
import com.prs.crm.domain.doc.DeptDoc;
import com.prs.crm.domain.doc.ShareDeptDoc;
import com.prs.crm.domain.hr.Person;
import com.prs.crm.service.doc.DeptDocService;
import com.prs.crm.service.doc.ShareDeptDocService;
import com.prs.crm.service.hr.PersonService;
import com.prs.crm.service.impl.BaseServiceImpl;

public class ShareDeptDocServiceImp extends BaseServiceImpl<ShareDeptDoc, ShareDeptDocDao> implements ShareDeptDocService {
	private PersonService personService;
	private DeptDocService deptDocService;
	@Override
	public Collection<ShareDeptDoc> getShareDeptDoc(DeptDoc deptDoc) {
		// TODO Auto-generated method stub
		return this.getDao().getShareDeptDoc(deptDoc);
	}

	@Override
	public void saveShareDeptDoc(ShareDeptDoc shareDeptDoc, String deptIds,  String sharePersons) {
		// TODO Auto-generated method stub
		this.getDao().deleteDeptDoc(shareDeptDoc.getDeptDoc());
		Collection<Person> persons = this.getPersonService().findPersons(ToolKits.convertStringAryToIntAry(sharePersons.split(",")));
		Collection<DeptDoc> deptDocs = new ArrayList<DeptDoc>();
		for(String deptId : deptIds.split(",")){
			deptDocs.add(deptDocService.get(Integer.parseInt(deptId)));
		}
		for(Person person:persons) {
			for(DeptDoc dept : deptDocs){
				ShareDeptDoc share = new ShareDeptDoc();
				share.setDeptDoc(shareDeptDoc.getDeptDoc());
				share.setEndTime(shareDeptDoc.getEndTime());
				share.setBeginTime(shareDeptDoc.getBeginTime());
				share.setRecorder(AppContext.getAccountContext().currentUser());
				share.setRecordTime(new Date());
				share.setPerson(person);
				share.setDeptDoc(dept);
				
				this.getDao().save(share);
			}
		}
	}

	public PersonService getPersonService() {
		return personService;
	}

	public void setPersonService(PersonService personService) {
		this.personService = personService;
	}

	public DeptDocService getDeptDocService() {
		return deptDocService;
	}

	public void setDeptDocService(DeptDocService deptDocService) {
		this.deptDocService = deptDocService;
	}

	@Override
	public Collection<ShareDeptDoc> getValidIssuePayroll(Person person) {
		// TODO Auto-generated method stub
		return this.getDao().getValidIssuePayroll(person);
	}

}
